package cn.kgc.dao;

import cn.kgc.util.DatabaseUtil;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

/**
 * 基础的dao，将来时所有的Dao实现类的父类
 */
public class BaseDao {
    PreparedStatement pmstm=null;
    //增删改通用方法
    public int execuseUpdate(Connection connection ,String sql,Object... params){

        try {
            pmstm= connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pmstm.setObject(i+1,params[i]);
            }
           int res= pmstm.executeUpdate();
            return res;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return 0;
    }



    //查询也是通用方法

    public ResultSet execuseQuery(Connection connection ,String sql,Object... params){

        try {
            pmstm= connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pmstm.setObject(i+1,params[i]);
            }
            return  pmstm.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    //查询也是通用方法

    public ResultSet execuseQuery2(Connection connection ,String sql,Object[] params){

        try {
            pmstm= connection.prepareStatement(sql);
            for (int i = 0; i < params.length; i++) {
                pmstm.setObject(i+1,params[i]);
            }
            return  pmstm.executeQuery();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

}
